package com.selune.online.ordering.system.project.customer.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.selune.online.ordering.system.project.api.base.entity.CustomerAddress;
import com.selune.online.ordering.system.project.api.base.exception.AddressException;
import com.selune.online.ordering.system.project.api.base.exception.ParamsException;
import com.selune.online.ordering.system.project.api.base.response.Response;
import com.selune.online.ordering.system.project.api.mapper.CustomerAddressMapper;
import com.selune.online.ordering.system.project.api.pojo.user.customer.dto.CustomerAddressDTO;
import com.selune.online.ordering.system.project.api.pojo.user.customer.vo.CustomerAddressVO;
import com.selune.online.ordering.system.project.api.service.customer.ICustomerAddressService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author xiaoyp
 * @date 2020/3/22
 * <p>
 * Description:
 */
@Service
@Slf4j
public class CustomerAddressServiceImpl implements ICustomerAddressService {
	
	@Resource
	private CustomerAddressMapper customerAddressMapper;
	
	@Override
	public Response<Boolean> saveAddress(Long userId, CustomerAddressDTO customerAddressDTO) {
		if (null == userId || null == customerAddressDTO) {
			log.error("ACCESS_LOG[]CustomerUserServiceImpl[]saveAddress[]address params is null!");
			throw new ParamsException("address.params.is.null");
		}
		
		CustomerAddress customerAddress = new CustomerAddress();
		BeanUtils.copyProperties(customerAddressDTO, customerAddress);
		customerAddress.setUserId(userId);
		
		int result = customerAddressMapper.insert(customerAddress);
		if (result == 1) {
			return Response.success("地址添加成功!", Boolean.TRUE);
		} else {
			return Response.error("地址添加失败");
		}
	}
	
	@Override
	public Response<Boolean> updateAddress(Long id, CustomerAddressDTO customerAddressDTO) {
		if (null == id || null == customerAddressDTO) {
			log.error("ACCESS_LOG[]CustomerUserServiceImpl[]updateAddress[]address params is null!");
			throw new ParamsException("address.params.is.null");
		}
		
		if (null == customerAddressMapper.selectById(id)) {
			log.error("ACCESS_LOG[]CustomerUserServiceImpl[]updateAddress[]address is not exist!");
			throw new AddressException("address.params.is.null");
		}
		
		CustomerAddress customerAddress = new CustomerAddress();
		BeanUtils.copyProperties(customerAddressDTO, customerAddress);
		customerAddress.setId(id);
		int result = customerAddressMapper.updateById(customerAddress);
		if (result == 1) {
			return Response.success("地址更新成功!", Boolean.TRUE);
		} else {
			return Response.error("地址更新失败");
		}
	}
	
	@Override
	public Response<List<CustomerAddressVO>> addressList(Long userId) {
		if (null == userId) {
			log.error("ACCESS_LOG[]CustomerUserServiceImpl[]addressList[]address params is null!");
			throw new ParamsException("address.params.is.null");
		}
		
		List<CustomerAddress> customerAddressList = customerAddressMapper.selectList(
			Wrappers.<CustomerAddress>lambdaQuery().eq(CustomerAddress::getUserId, userId)
		);
		
		List<CustomerAddressVO> customerAddressVOList = new ArrayList<>(customerAddressList.size());
		customerAddressList.forEach(customerAddress -> {
			CustomerAddressVO customerAddressVO = new CustomerAddressVO();
			BeanUtils.copyProperties(customerAddress, customerAddressVO);
			customerAddressVOList.add(customerAddressVO);
		});
		
		return Response.success(customerAddressVOList);
	}
	
	@Override
	public Response<Boolean> deleteAddressById(Long id) {
		if (null == id) {
			log.error("ACCESS_LOG[]CustomerUserServiceImpl[]deleteAddressById[]address params is null!");
			throw new ParamsException("address.params.is.null");
		}
		
		int result = customerAddressMapper.deleteById(id);
		if (result == 1) {
			return Response.success("地址删除成功!", Boolean.TRUE);
		} else {
			return Response.error("地址删除失败");
		}
	}
	
	@Override
	public Response<Boolean> deleteAddressByIds(List<Long> ids) {
		if (CollectionUtils.isEmpty(ids)) {
			log.error("ACCESS_LOG[]CustomerUserServiceImpl[]deleteAddressByIds[]address params is null!");
			throw new ParamsException("address.params.is.null");
		}
		
		int result = customerAddressMapper.deleteBatchIds(ids);
		if (result >= 0) {
			return Response.success("地址删除成功!", Boolean.TRUE);
		} else {
			return Response.error("地址删除失败");
		}
	}
}
